home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9848 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  59 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP! Modifying the EOF in a file!
  5. Date: Wed, 13 Mar 96 20:41:51 GMT
  6. Organization: none
  7. Message-ID: <826749711snz@genesis.demon.co.uk>
  8. References: <4i585k$4ia@news.netam.net> <4i58ki$uoh@grail.fgi.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4i58ki$uoh@grail.fgi.net>
  15.            bwendlin@I_should_put_my_domain_in_etc_NNTP_INEWS_DOMAIN
  16.            "Physics Lizard" writes:
  17.  
  18. >The Bowling Green Connection inexplicably wrote:
  19. >} I am having trouble shortening the size of a text file...
  20. >} Suppose I have a text file with 3 lines of data, then I
  21. >} run this program:
  22. >
  23. >} void main() {
  24. >}    FILE *dat;
  25. >}    dat=fopen("file.txt", "r+");
  26. >}    fprintf(dat, "Hello! %c", EOF);
  27. >}    fclose(dat);
  28. >} }
  29. >
  30. >} Why doesn't the EOF character chop off the remaining two lines?
  31. >} When I print out the file after running this program, the ONLY thing
  32. >} that changed was the first few characters.. The other two lines
  33. >} still remain..  HELP!
  34.  
  35. Because end-of-file is not a character in C, it is a condition or status
  36. a stream can enter. Certain input function like getchar() indicate this
  37. conditiion to the caller by returning the value of the EOF macro. The
  38. reason the getchar() returns int is because EOF is not necessarily
  39. a valid character value. Therefore trying to write it out as a character
  40. makes no sense.
  41.  
  42. C provides no method of truncating a file at an arbitrary point. When you
  43. open a file using modes "w", "wb", "w+" or "w+b" then the file is truncated
  44. to zero length. To do what you want you typically create a second empty file
  45. and then copy as much as you need of the first file across to it.
  46.  
  47. >You are opening the file read only.  In order to do what you are 
  48. >wanting, why don't you just open the file as write?  Then, everything
  49. >in the file will be overwritten.
  50.  
  51. No, the "r+" mode open the file for update i.e. both reading and writing.
  52. It doesn't truncate the file however.
  53.  
  54. -- 
  55. -----------------------------------------
  56. Lawrence Kirby | fred@genesis.demon.co.uk
  57. Wilts, England | 70734.126@compuserve.com
  58. -----------------------------------------
  59.